package com.meme.util;

import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.apache.log4j.Logger;


import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Window;
import java.awt.Dialog;
import java.awt.FileDialog;
import java.io.File;



public class GuiUtil 
{
	static Logger logger = Logger.getLogger (GuiUtil.class);
	
	
	public static Frame getTopFrame (Component child)
	{
		Component  parent = child;
		while ( parent != null )
		{			
			if ( parent instanceof Frame )
				return (Frame) parent;

			parent = parent.getParent();
		}
		
		return null;
	}

	
	static String sLastFilename = null;
	
	public static String popupFileChooser (Container window, String title, boolean bLoad, String extension, String fileDescription, String defaultFileName)
	{
		return popupFileChooser (window, title, bLoad, new String[]{extension}, fileDescription, defaultFileName);
	}
	
	static JFileChooser _JFileChooser;
	public static String popupFileChooser (Container window, String title, boolean bLoad, String[] extensions, String fileDescription, String defaultFileName)
	{
		if (_JFileChooser == null || ! bLoad)
		{
			_JFileChooser = new JFileChooser();
		}
		
	    if ( bLoad && defaultFileName != null && defaultFileName.trim().length () > 0)
	    {
	    	if (sLastFilename == null)
	    		_JFileChooser.setCurrentDirectory (new File (defaultFileName));
	    	else
	    	{
		    	File defalutFile = new File (defaultFileName);

		    	if (! defalutFile.isDirectory())
	    			defalutFile = defalutFile.getParentFile();

	    		File lastFile = new File (sLastFilename);
	    		if (! lastFile.isDirectory())
	    			lastFile = lastFile.getParentFile();
	    		
	    		if (defalutFile != null && ! defalutFile.equals(lastFile))
	    		{
	    			_JFileChooser.setCurrentDirectory (new File (defaultFileName));
	    		}
	    	}
	    }
	    else
	    {
	    	if (sLastFilename != null)
	    	{
	    		File lastFile = new File (sLastFilename);
	    		if (! lastFile.isDirectory())
	    			lastFile = lastFile.getParentFile();
	    		_JFileChooser.setCurrentDirectory (lastFile);
	    	}
	    }

		JFileChooser chooser = _JFileChooser;


		
		
	    
	    chooser.resetChoosableFileFilters ();
	    if (extensions != null && extensions.length > 0)
	    {
	    	FileNameExtensionFilter filter;
		    filter = new FileNameExtensionFilter ( fileDescription, extensions );
		    chooser.setFileFilter(filter);
	    }
	    chooser.setDialogType (bLoad ? JFileChooser.OPEN_DIALOG : JFileChooser.SAVE_DIALOG);

	    int returnVal = chooser.showDialog ((Component)window, null);
	    if(returnVal == JFileChooser.APPROVE_OPTION) 
	    {
	    	String filename = chooser.getSelectedFile().getAbsolutePath();
	    	if (extensions.length == 1)
	    	{
	    		if (filename.lastIndexOf("." + extensions[0]) < 0)
	    			filename = filename + "." + extensions[0];
	    	}
	    	
	    	sLastFilename = filename;
	    	return filename;
	    }
	    else
	    {
	    	return null;
	    }			
	}
	
}
